import CryptoJS from 'crypto-js';

const key = 'championword';  // 要与加解密要一致
const keyHex = CryptoJS.enc.Utf8.parse(key);

// 加密
const encryptDES = (message) => {
  if (message) {
    var encrypt = CryptoJS.DES.encrypt(message, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypt.toString();
  } else {
    return '';
  }
};

// 解密
const decryptDES = (ciphertext) => {
  if (ciphertext) {
    let decrypted = CryptoJS.DES.decrypt(ciphertext, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
  } else {
    return '';
  }
};

export {
  encryptDES,
  decryptDES
}